<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>受控组件</title>
</head>
<body>
  <div id="test"></div>

  <!-- 引入库：先引入核心库，再引入操作DOM的扩展库 -->
  <script type="text/javascript" src="../js/react.development.js"></script>
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <!-- 将jsx转为js -->
  <script type="text/javascript" src="../js/babel.min.js"></script>
  <script type="text/babel">    //必须写babel，因为默认js
    class Login extends React.Component{

      //初始化
      state ={
        username:'',    //用户名
        password:''     //密码
      }

      //保存用户、密码到状态中
      changeUsername = (event)=>{
        this.setState({username:event.target.value})
      }
      changePassword = (event)=>{
        this.setState({password:event.target.value})
      }

      //表单提交的回调
      handleSubmit = (event)=>{
        event.preventDefault()
        const {username,password} = this.state
        alert(`你输入的用户名：${username}，你输入的密码：${password}`)
      }

      render(){
        return(
          <form onSubmit={this.handleSubmit}>
            用户名：<input onChange={this.changeUsername} type="text" name="username"/>
            密码<input onChange={this.changePassword} type="password" name="password"/><br/>
            <button>登录</button>
          </form>
        )
      }
    }

    ReactDOM.render(<Login/>, document.getElementById('test'))
  </script>
</body>
</html>